package com.common.feint;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FeintException {

    private final static Map<String, Throwable> exceptionMap = new ConcurrentHashMap<>();

    private static boolean throwExceptionSwitch = false;

    public static void feintJump(String key) {
        if (throwExceptionSwitch){
            Throwable ex = exceptionMap.get(key);
            if (ex != null){
                FeintException.<RuntimeException>sneakyExceptionInner(ex);
            }
        }
    }

    public static void putException(String key, Throwable ex){
        exceptionMap.put(key, ex);
    }

    private static <T extends Throwable> T sneakyExceptionInner(Throwable ex) throws T {
        throw (T)ex;
    }

    public static boolean isThrowExceptionSwitch(){
        return throwExceptionSwitch;
    }

    public static void setThrowExceptionSwitch(boolean throwExceptionSwitch){
        FeintException.throwExceptionSwitch = throwExceptionSwitch;
    }

}
